//
//  AccountProtectionViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/12.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "AccountProtectionViewController.h"

#import "AccountProtectionTableViewCell_1.h"
#import "AccountProtectionTableViewCell_2.h"

@interface AccountProtectionViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;
//头视图
@property (nonatomic, strong) UIView * view_Top;
@property (nonatomic, strong) UIView * view_Line;
@property (nonatomic, strong) UIButton * btn_equipment;
@property (nonatomic, strong) UIButton * btn_loginDaily;

@end

@implementation AccountProtectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"账号保护";
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 90, 15)];
    lbl_return.text = @"账号与安全";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 105, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //头视图
    [self p_headView];
    
    //
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 40, SCREEN_WIDTH, SCREEN_HEIGHT - 104) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = YES;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[AccountProtectionTableViewCell_1 class] forCellReuseIdentifier:@"equipment"];
    
    [self.tableView registerClass:[AccountProtectionTableViewCell_2 class] forCellReuseIdentifier:@"loginDaily"];
    
    
    //刷新
    __unsafe_unretained UITableView * tableView = self.tableView;
    //    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        // 结束刷新
        [tableView.mj_header endRefreshing];
    }];
    
    // 马上进入刷新状态
    [tableView.mj_header beginRefreshing];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
    // 上拉加载
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        // 结束刷新
        [tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - 加载数据

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.view_Line.frame.origin.x == 0)
    {
        return 5;
    }
    else
    {
        return 13;
    }
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.view_Line.frame.origin.x == 0)
    {
        return 65;
    }
    else
    {
        return 90;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.view_Line.frame.origin.x == 0)
    {
        AccountProtectionTableViewCell_1 * cell = [tableView dequeueReusableCellWithIdentifier:@"equipment" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.name.text = @"iPhone4";
        cell.time.text = @"最近登录: 2016-09-12 08:11";
        
        return cell;
    }
    else
    {
        AccountProtectionTableViewCell_2 * cell = [tableView dequeueReusableCellWithIdentifier:@"loginDaily" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.lbl_time.text = [NSString stringWithFormat:@"最近登录: 2016-09-%02ld 08:11",30 - indexPath.row];
        cell.lbl_telType.text = @"设备类型: Mac桌面客户端";
        cell.lbl_success.text = @"登录动作: 登录成功";
        
        return cell;
    }
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.view_Line.frame.origin.x == 0)
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"取消该设备的授权" message:@"授权时间: 2016-05-23 08:22" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alert animated:YES completion:^{
        }];
        
        UIAlertAction * action_cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action_cancel];
        
        UIAlertAction * action_ok = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"掉接口,取消该设备授权");
            
        }];
        [alert addAction:action_ok];
    }
}

#pragma mark - 头视图
- (void )p_headView
{
    self.view_Top = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 40)];
    self.view_Top.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.view_Top];
    
    //设备管理
    self.btn_equipment = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_equipment.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, 40);
    [self.btn_equipment setTitle:@"设备管理" forState:(UIControlStateNormal)];
    [self.btn_equipment setTitleColor:[UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
    self.btn_equipment.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view_Top addSubview:self.btn_equipment];
    [self.btn_equipment addTarget:self action:@selector(btn_equipmentAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //登录日志
    self.btn_loginDaily = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_loginDaily.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 40);
    [self.btn_loginDaily setTitle:@"登录日志" forState:(UIControlStateNormal)];
    [self.btn_loginDaily setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    self.btn_loginDaily.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view_Top addSubview:self.btn_loginDaily];
    [self.btn_loginDaily addTarget:self action:@selector(btn_loginDailyAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //
    self.view_Line = [[UIView alloc] init];
    self.view_Line.frame = CGRectMake(0, 38, SCREEN_WIDTH / 2, 2);
    self.view_Line.backgroundColor = [UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f];
    [self.view_Top addSubview:self.view_Line];
}

#pragma mark - 头部点击按钮
- (void )btn_equipmentAction:(UIButton *)sender
{
//    NSLog(@"点击设备管理");
    
    if(self.view_Line.frame.origin.x != 0)
    {
        [self.btn_equipment setTitleColor:[UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
        [self.btn_loginDaily setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [UIView animateWithDuration:0.4f animations:^{
            self.view_Line.frame = CGRectMake(0, 38, SCREEN_WIDTH / 2, 2);
        }];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}

- (void )btn_loginDailyAction:(UIButton *)sender
{
//    NSLog(@"点击登录日志");
    
    if(self.view_Line.frame.origin.x == 0)
    {
        [self.btn_equipment setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [self.btn_loginDaily setTitleColor:[UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f] forState:(UIControlStateNormal)];
        [UIView animateWithDuration:0.4f animations:^{
            self.view_Line.frame = CGRectMake(SCREEN_WIDTH / 2, 38, SCREEN_WIDTH / 2, 2);
        }];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}








@end
